<template>
  <div class="execution">
    <basic-container>
      <el-row>
        <el-col :span="20">
          <avue-form v-model="searchForm" :option="searchFormOption">
          </avue-form>
        </el-col>
        <el-col :span="4" style="text-align: center;">
          <el-button type="primary" @click="submit" :loading="isSubmit">
            一键确认
          </el-button>
        </el-col>
      </el-row>
      <el-tabs v-model="active" type="card">
        <el-tab-pane label="配票与配备用金确认" name="1">
          <ticket-petty-add ref="1"></ticket-petty-add>
        </el-tab-pane>
        <el-tab-pane label="售票员日结算数据录入确认" name="2">
          <conductor-settle ref="2"></conductor-settle>
        </el-tab-pane>
        <el-tab-pane label="营销车票数据录入确认" name="3">
          <marketing-ticket ref="3"></marketing-ticket>
        </el-tab-pane>
        <el-tab-pane label="乘客事务确认" name="4">
          <passenger-work-confirm ref="4"></passenger-work-confirm>
        </el-tab-pane>
      </el-tabs>
    </basic-container>
  </div>
</template>

<script>
  import ticketPettyAdd from "./ticket-petty-add";
  import { checkPwd } from "@/api/admin/user";
  import conductorSettle from "./conductor-settle";
  import marketingTicket from "./marketing-ticket";
  import passengerWorkConfirm from "./passenger-work-confirm";
  import {
    getNeedConfirmData,
    confirmAll
  } from "@/api/subwaystm/swstticketpettyadd";
  import { dateFormat } from "@/util/date";
  export default {
    name: "confirmInfo",
    data() {
      return {
        rStatus: {
          deviceAttribution: '1'
        },
        active: "1",
        searchForm: {
          date: dateFormat(new Date()).substr(0, 10),
          nsJobNo: "",
          nsShift: ""
        },
        isSubmit: false,
        stTicketPettyAdd: {},
        searchFormOption: {
          labelWidth: 100,
          submitBtn: false,
          emptyBtn: false,
          group: [
            {
              label: "",
              column: [
                {
                  label: "时间",
                  prop: "date",
                  type: "date",
                  span: 8,
                  format: "yyyy-MM-dd",
                  valueFormat: "yyyy-MM-dd",
                  clearable: false,
                  change: this.getData
                },
                {
                  label: "售票员",
                  prop: "nsJobNo",
                  type: "select",
                  span: 8,
                  dicUrl: "/admin/user/page",
                  props: {
                    label: "nikename",
                    value: "userCode",
                    res: "data.records"
                  },
                  filterable: true,
                  dicQuery: {
                    size: -1
                    //TODO 过滤售票员
                  },
                  clearable: false,
                  change: this.getData
                },
                {
                  label: "班次",
                  prop: "nsShift",
                  type: "select",
                  span: 8,
                  clearable: false,
                  dicUrl: "/admin/dict/type/stm_conductor_shift",
                  change: this.getData
                }
              ]
            }
          ]
        }
      };
    },
    components: {
      ticketPettyAdd,
      conductorSettle,
      marketingTicket,
      passengerWorkConfirm
    },
    watch: {
      active() {
        this.getData();
      }
    },
    methods: {
      getData() {
        this.$nextTick(() => {
          if(
            !this.validatenull(this.searchForm.date) &&
            !this.validatenull(this.searchForm.nsJobNo) &&
            !this.validatenull(this.searchForm.nsShift)
          ){
            this.$refs[this.active].getData(this.searchForm);
          }
        });
      },
      async getTicketData(searchForm) {
        await getNeedConfirmData(searchForm).then(response => {
          let s = response.data.data;
          if(!this.validatenull(s.stTicketPettyAdd)){
            this.rStatus = s.stTicketPettyAdd;
          }
        });
      },
      submit() {
        this.rStatus.deviceAttribution = '1';
        if (!this.searchForm.nsJobNo) {
          this.$message.info("请选择售票员再进行确认");
          return;
        }
        if(!this.searchForm.nsShift){
          this.$message.info("请选择班次再进行确认！");
          return;
        }
        this.getTicketData(this.searchForm).then(res=>{
          if(this.rStatus.deviceAttribution == '0'){
            this.$confirm("该售票员有未归还的配票与配备用金数据,是否继续确认", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.isSubmit = true;
              this.$prompt("请输入售票员密码", "确认", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                inputType: "password",
                inputValidator: val => {
                  if (!val) return "请输入密码";
                }
              })
                .then(({ value }) => {
                  return checkPwd(this.searchForm.nsJobNo, value);
                })
                .then(response => {
                  var result = response.data.data;
                  if (result) {
                    return;
                  } else {
                    this.$message.error("密码错误");
                    return Promise.reject("密码错误");
                  }
                })
                .then(() => {
                  return confirmAll(this.searchForm);
                })
                .then(() => {
                  this.getData();
                  this.$message.success("确认成功");
                  this.isSubmit = false;
                })
                .catch(error => {
                  console.log(error);
                  this.isSubmit = false;
                });
            })
          }else{
            this.isSubmit = true;
            this.$prompt("请输入售票员密码", "确认", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              inputType: "password",
              inputValidator: val => {
                if (!val) return "请输入密码";
              }
            })
              .then(({ value }) => {
                return checkPwd(this.searchForm.nsJobNo, value);
              })
              .then(response => {
                var result = response.data.data;
                if (result) {
                  return;
                } else {
                  this.$message.error("密码错误");
                  return Promise.reject("密码错误");
                }
              })
              .then(() => {
                return confirmAll(this.searchForm);
              })
              .then(() => {
                this.getData();
                this.$message.success("确认成功");
                this.isSubmit = false;
              })
              .catch(error => {
                console.log(error);
                this.isSubmit = false;
              });
          }
        })
      }
    }
  };
</script>

<style></style>
